﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Torch.WL.Web;
using Torch.WL.Business;
using Torch.WL.Entity;
using Torch.FrameWork.Common;
using Torch.FrameWork.Utility;


namespace Torch.WL.Web.Admin.Modubles
{
    public partial class EditUserPage : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UserHandle.InitModule("admin_ListUsers");
                if (UserHandle.ValidationHandle(PermissionTag.View))
                {
                    if (!UserHandle.ValidationHandle(PermissionTag.Edit))
                    {
                        Btn_ok.Visible = false;
                    }

                    BindUserGroup();

                    if (Request.QueryString["uid"] != null)
                    {
                        txt_name.Enabled = false;
                        GetUser(int.Parse(Request.QueryString["uid"].ToString()));
                    }
                    else
                    {

                    }

                }
                else
                {
                    GotoErrPage("无权限!"); 
                }

            }
        }

        bool AddMode
        {
            get 
            {
                if (Request.QueryString["uid"] ==null)
                    return true;

                return false;
            }
        }        

        protected void GetUser(int id)
        {
            UserLogic bll = new UserLogic();
            UserEntity model = new UserEntity();
            model = bll.GetUserModel(id);
            if (model == null)
            {
                LogEvent.LogError("EditUserPage.aspx.cs无效用户ID{0}", id);
                return;
            }

            uid.Text = id.ToString();
            txt_name.Text = model.UserName;
            txtShowName.Text = model.ShowName;
            GroupList.SelectedValue = model.UserGroup.ToString();

            txt_email.Text = model.Email;
            StateList.SelectedValue = model.Status.ToString();
            Lab_time1.Text = model.CreateTime.ToString("yyyy-MM-dd HH:mm:ss");
            ArrayList rid=model.RoleID;
            for (int i = 0; i < rid.Count; i++)
            {
                string[] r= rid[i].ToString().Split(',');
                RoleList.Items.Add(new ListItem(r[1], r[0]));
            }

        }

        void BindUserGroup()
        {
            UserGroupLogic ugbll = new UserGroupLogic();
            DataView dvList = new DataView(ugbll.GetUserGroupList("").Tables[0]);
            LoadGroupList("0", 0, dvList);
        }

        /// <summary>
        /// 加载用户分组
        /// </summary>
        /// <param name="UG_TID">分类上级ID</param>
        /// <param name="Depth">分类级别深度</param>
        protected void LoadGroupList(string UG_TID, int Depth, DataView dvList)
        {
            dvList.RowFilter = "UG_SuperiorID=" + UG_TID;  //过滤父节点 
            foreach (DataRowView dv in dvList)
            {
                string depth = string.Empty;
                for (int i = 0; i < Depth; i++)
                {
                    depth = depth + "-";
                }
                GroupList.Items.Add(new ListItem(depth + dv["UG_Name"].ToString(), dv["UG_ID"].ToString()));

                LoadGroupList(dv["UG_ID"].ToString(), int.Parse(dv["UG_Depth"].ToString()) + 1, dvList);  //递归 
            }
        }

        protected void Btn_ok_Click(object sender, EventArgs e)
        {
            int rowseffected = -1;

            UserLogic bll = new UserLogic();
            UserEntity model = new UserEntity();

            model.UserName = txt_name.Text.Trim();
            model.ShowName = txtShowName.Text;
            model.UserGroup = int.Parse(GroupList.SelectedItem.Value);
            model.Email = txt_email.Text;
            model.Status = int.Parse(StateList.SelectedItem.Value);
            if (AddMode)
            {
                if (model.UserName.Length == 0)
                {
                    ShowMsg(false, "请输入用户名!");
                    return;
                }

                if (bll.UserExists(model.UserName))
                {                  
                    ShowMsg(false, "用户名已存在,无法保存!");
                    return;
                }

                if (txtPassWord.Text.Trim().Length == 0)
                {
                    //strinfo.InnerHtml = JScript.errinfo("请输入用户密码!");
                    //strinfo.Attributes["class"] = "w_error";
                    ShowMsg(false, "请输入用户密码!");
                    return; 
                }
                model.Password = CryptMethod.MD5(txtPassWord.Text.Trim(), 32);

                rowseffected = bll.CreateUser(model);
            }
            else
            {
                model.UserID = int.Parse(uid.Text);
                rowseffected = bll.UpdateUser(model);

                if (txtPassWord.Text.Trim().Length > 0)
                {
                    bll.UpdateUserPassWord(model.UserID, CryptMethod.MD5(txtPassWord.Text.Trim(), 32));
                }
            }

            if (rowseffected >= 1)
            {
                //strinfo.InnerHtml = JScript.errinfo("用户信息保存成功!");
                ShowMsg(true , "用户信息保存成功!");
            }
            else
            {
                //strinfo.InnerHtml = JScript.errinfo("用户信息保存失败!");
                ShowMsg(false, "用户信息保存失败!");
            }
        }

        void ShowMsg(bool success , string msg)
        {
            strinfo.InnerHtml = msg;
            strinfo.Attributes["class"] =success?"w_info":"w_error";
            strinfo.Visible = true;
        }
    }
}
